package com.gipson.amc.service.basicdata;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.gipson.amc.entity.*;
import com.gipson.amc.mapper.AuthorityMapper;
import com.gipson.amc.mapper.CompanyMapper;
import com.gipson.amc.utils.security.ShiroUtils;
import com.gipson.amc.utils.strprocess.ArrayUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公司管理 服务实现类
 * </p>
 *
 * @author xsyuan
 * @since 2020-05-09
 */
@Service
public class CompanyService extends ServiceImpl<CompanyMapper, Company> {
	
	public Page<Company> getCompanyPage(Company company, PageByDataTable pageByDataTable) {
		Page<Company> page = new Page<Company>().setCurrent(pageByDataTable.getPageindex()).setSize(pageByDataTable.getPagesize());
		QueryWrapper<Company> queryWrapper = new QueryWrapper<Company>()
				.like(StringUtils.isNotBlank(company.getCompanyCode()), "Company_CODE", company.getCompanyCode())
				.like(StringUtils.isNotBlank(company.getCompanyName()), "Company_NAME", company.getCompanyName())
				.eq("DEL_FLAG", company.getDelFlag() == null ? 0 : company.getDelFlag())
				.orderByAsc("SORT");
		baseMapper.selectPage(page, queryWrapper);
		return page;
	}
	
	/**
	 * 删除
	 *
	 * @param ids
	 */
	public void delBatch(String ids) {
		String[] idStr = ids.split(",");
		List<String> idList = Arrays.asList(idStr);
		List<Company> companyList = baseMapper.selectBatchIds(idList);
      /*  List<String> deleteByIds = new ArrayList<>();
        for (Company company : companyList){
            deleteByIds.add(company.getId());
        }
        baseMapper.deleteBatchIds(deleteByIds);*/
		for (Company company : companyList) {
			company.setDelFlag(1);
		}
		updateBatchById(companyList, 1000);
	}
	
	@Transactional
	public ResponseMessage saveOrUpdateByCompany(Company company) {
		User user = ShiroUtils.getCurrentUser();
		if (user != null) {
			if (StringUtils.isNotBlank(company.getId())) {
				company.setUpdateUser(user.getId());
				company.setUpdateTime(new Date());
				updateById(company);
				return ResponseMessage.ok("修改成功");
			} else {
				List<Company> checkCompanyCode = list(Wrappers.<Company>lambdaQuery().eq(Company::getCompanyCode, company.getCompanyCode()));
				List<Company> checkCompanyName = list(Wrappers.<Company>lambdaQuery().eq(Company::getCompanyName, company.getCompanyName()));
				String msg = "";
				//判断公司编号是否重复
				if (CollectionUtils.isNotEmpty(checkCompanyCode)) {
					msg = "公司编号已存在！";
					return ResponseMessage.error(msg);
				}
				//判断公司名称是否重复
				if (CollectionUtils.isNotEmpty(checkCompanyName)) {
					msg = "公司名称已存在！";
					return ResponseMessage.error(msg);
				}
				company.setCreateUser(user.getId());
				company.setCreateTime(new Date());
				save(company);
				return ResponseMessage.ok("新增成功");
			}
		}
		return ResponseMessage.error("登陆用户不存在，请重新登陆！");
	}
	
	public Company findCompanyByCompanyId(String id) {
		return baseMapper.findCompanyByCompanyId(id);
	}
	
	/**
	 * @description 通过ID查询
	 * @param
	 * @author: xsyuan 2020/7/7 10:43
	 * @return
	 */
	public Company getById(String id){
		Company company = baseMapper.selectOne(Wrappers.<Company>lambdaQuery().eq(Company::getId,id).eq(Company::getDelFlag,0));
		if(company != null){
			if(StringUtils.isNotBlank(company.getParentId())){
				Company parentCompany = baseMapper.selectOne(Wrappers.<Company>lambdaQuery().eq(Company::getId,company.getParentId()).eq(Company::getDelFlag,0));
				company.setParentCompanyName(parentCompany.getCompanyName());
			}
			
		}else{
			company = new Company();
		}
		return company;
	}
	
	/**
	 * 根据companyCode获取company。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-05-21
	 */
	public Company getCompanyByCode(String companyCode) {
		Wrapper<Company> wrapper = Wrappers.<Company>lambdaQuery()
				.eq(Company::getCompanyCode, companyCode)
				.eq(Company::getDelFlag, 0);
		Company company = baseMapper.selectOne(wrapper);
		return company;
	}
	
	/**
	 * 根据companyCodeList获取company列表。
	 *
	 * @param
	 * @return
	 * @author cjw 2020-06-02
	 */
	public List<Company> getCompanyByCode(List<String> companyCodeList) {
		Wrapper<Company> wrapper = Wrappers.<Company>lambdaQuery()
				.in(!ArrayUtil.isEmpty(companyCodeList), Company::getCompanyCode, companyCodeList)
				.eq(Company::getDelFlag, 0)
				.orderByAsc(Company::getCompanyCode);
		List<Company> companyList = baseMapper.selectList(wrapper);
		return companyList;
	}
	
	/**
	 * 获取company的有效的列表。
	 * @param
	 * @return
	 * @author cjw 2020-07-21
	 */
	public List<Company> getCompany(){
		Wrapper wrapper= Wrappers.<Company>lambdaQuery()
				.eq(Company::getDelFlag, 0)
				.orderByAsc(Company::getCompanyCode);
		List<Company> companyList= baseMapper.selectList(wrapper);
		return companyList;
	}
	
}
